﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace AsyncReturnValueTpye
{
    static class DoAsyncStuffClass
    {

        private static string GetName(string Name, int Year)//方法
        {

            return Name + "  " + Year;
        }

        private static async Task GetTaskAsync(string Name, int Year)//任务
        {
            string Name1 = await Task.Run(() => GetName(Name, Year));
            Console.WriteLine($"Name:{Name}  Year:{Year}岁");
        }

        static public void CallAsyncMethodReturnTask()
        {
            Console.WriteLine("异步方法Task：");
            Task t = GetTaskAsync("田", 56);
            t.Wait();//等待程序完成在执行下一步
            Console.WriteLine("处理1");

            Console.WriteLine();
            Task task = GetTaskAsync("三轮", 55);
            Console.WriteLine("不等待完成，直接执行下一个");

            //Console.WriteLine();
            Console.WriteLine("异步方法ValueTask:");
            ValueTask<string> value = GetAsyncAndAwait("摩托", 25);
            Console.WriteLine($"ValueTask1:{value.Result}");
            Console.WriteLine("ValueTask处理");

        }

        private static async ValueTask<string> GetAsyncAndAwait(string Name, int Year)
        {
            if (Name == null)
            {
                return Convert.ToString(Year);
            }

            if (Year <= 0)
            {
                return Name;
            }

            string Name1 = await Task.Run(() => GetName(Name, Year));

            return Name1;

        }




    }
}
